package com.xianyu.ssoserver.controller;

import com.menghao.sso.server.exception.InvalidTicketException;
import com.menghao.sso.server.service.AuthorizationService;
import com.menghao.sso.server.util.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>注销Controller.<br>
 *
 * @author menghao.
 * @version 2017/11/20.
 */
@Controller
@RequestMapping(CommonUtils.LOGOUT_URL)
public class LogoutController {

    @Autowired
    private AuthorizationService authorizationService;

    /**
     * 用户退出
     *
     * @param request (tgtId)
     * @return ResponseEntity<String>
     */
    @GetMapping
    public ResponseEntity<Boolean> logout(HttpServletRequest request) {
        String grantingTicket = request.getParameter(com.menghao.sso.client.util.CommonUtils.TGT_ID);
        try {
            authorizationService.destroyTicketGrantingTicket(grantingTicket);
            return new ResponseEntity<Boolean>(true, HttpStatus.OK);
        } catch (InvalidTicketException e) {
            return new ResponseEntity<Boolean>(false, HttpStatus.NOT_FOUND);
        }
    }
}
